package org.dawfinn.droiditarian.view;

import org.dawfinn.droiditarian.R;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.widget.RelativeLayout;

public class UpdateView extends RelativeLayout {

	public enum State {
		LOADING, IDLE, DISABLED;
	}

	private AnimationSet fadeIn;
	private AnimationSet fadeOut;
	private State state = State.IDLE;
	private View currentlyVisible;
	private View loadingView;
	private View idleView;
	private View disabledView;

	public UpdateView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}

	public UpdateView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public UpdateView(Context context) {
		super(context);
		init(context);
	}

	private void init(Context context) {
		((LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
				R.layout.update_view, this);
		loadingView = findViewById(R.id.loadingView);
		idleView = findViewById(R.id.updateText);
		disabledView = findViewById(R.id.disabledText);

		currentlyVisible = idleView;

		fadeIn = (AnimationSet) AnimationUtils.loadAnimation(context,
				R.anim.fade_in);
		fadeOut = (AnimationSet) AnimationUtils.loadAnimation(context,
				R.anim.fade_out);
	}

	public void setState(State state) {
		if (state != this.state) {
			currentlyVisible.startAnimation(fadeOut);
			currentlyVisible.setVisibility(INVISIBLE);
			switch (state) {
			case IDLE:
				currentlyVisible = idleView;
				break;
			case LOADING:
				currentlyVisible = loadingView;
				break;
			case DISABLED:
				currentlyVisible = disabledView;
			}
			currentlyVisible.setVisibility(VISIBLE);
			currentlyVisible.startAnimation(fadeIn);
		}
		this.state = state;
	}

	public State getState() {
		return state;
	}
}
